/*********************************************************************
*
*  Upload seetings dialog box. includes HTTP and FTP protocol selection
*  and configuring IP address, user name and password .
*
**********************************************************************
* File Name:       UploadSettings.java
* Complier:        JAVA version "1.6.0_20 and onwards"
* IDE :            NetBeans
*/

/*
Copyright (C) 2012-2023, Microchip Technology Inc., and its subsidiaries. All rights reserved.

The software and documentation is provided by microchip and its contributors
"as is" and any express, implied or statutory warranties, including, but not
limited to, the implied warranties of merchantability, fitness for a particular
purpose and non-infringement of third party intellectual property rights are
disclaimed to the fullest extent permitted by law. In no event shall microchip
or its contributors be liable for any direct, indirect, incidental, special,
exemplary, or consequential damages (including, but not limited to, procurement
of substitute goods or services; loss of use, data, or profits; or business
interruption) however caused and on any theory of liability, whether in contract,
strict liability, or tort (including negligence or otherwise) arising in any way
out of the use of the software and documentation, even if advised of the
possibility of such damage.

Except as expressly permitted hereunder and subject to the applicable license terms
for any third-party software incorporated in the software and any applicable open
source software license terms, no license or other rights, whether express or
implied, are granted under any patent or other intellectual property rights of
Microchip or any third party.
*/


package MicrochipMPFS;
import javax.swing.*;
//import javax.swing.JRootPane;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.Action;

public class UploadSettings extends javax.swing.JDialog {
    MainMPFS MPFS2;
    public String str;
    public String uploadAddress;
    public String uploadPath;
    public String username;
    public String password;
    ImageIcon icon = new ImageIcon(getClass().getResource("/Resource/mchpIcon.png"));
    /** Creates new form UploadSettings */
    public UploadSettings(javax.swing.JFrame parent, boolean modal){
        super(parent, modal);        
        MPFS2=(MainMPFS)parent;
        this.setIconImage(icon.getImage());
        initComponents();
        toolTipUploadSettings();
        txtIPAddress.setText("MCHPBOARD");
        txtPass.setText("microchip");
        txtuploadPath.setText("mpfsupload");
        txtUserName.setText("admin");
        username = "admin";
        uploadAddress = "MCHPBOARD";
        password = "microchip";
        uploadPath = "mpfsupload";
        radBtnHttp.setSelected(true);
        setUploadSettingsPath();

        UploadSettingKeyEventActionIntialization();

       /* Action  ESCactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);
            dispose();
          }
        };
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
        JComponent comp = this.getRootPane();
        comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ESCAPE");
        ActionMap actionMap = comp.getActionMap();
        actionMap.put("ESCAPE", ESCactionListener); 
        */
//        comp.getRootPane().setDefaultButton(bonOK);
//        comp.getRootPane().setDefaultButton(botDefault);
//        comp.getRootPane().setDefaultButton(botCancel);  
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblDevAddress = new javax.swing.JLabel();
        txtIPAddress = new javax.swing.JTextField();
        lblUploadPath = new javax.swing.JLabel();
        txtuploadPath = new javax.swing.JTextField();
        lblUsername = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        txtPass = new javax.swing.JTextField();
        lblPass = new javax.swing.JLabel();
        bonOK = new javax.swing.JButton();
        botDefault = new javax.swing.JButton();
        botCancel = new javax.swing.JButton();
        radBtnHttp = new javax.swing.JRadioButton();
        radBtnFTP = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MPFS Upload Settings");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(285, 220));
        setModal(true);
        setResizable(false);

        lblDevAddress.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11)); // NOI18N
        lblDevAddress.setText("Device Address:");

        txtIPAddress.setText("MCHPBOARD");

        lblUploadPath.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11)); // NOI18N
        lblUploadPath.setText("Upload Path:");

        txtuploadPath.setText("mpfsupload");

        lblUsername.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11)); // NOI18N
        lblUsername.setText("User Name:");

        txtUserName.setText("admin");

        txtPass.setText("microchip");

        lblPass.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11)); // NOI18N
        lblPass.setText("Password:");

        bonOK.setText("OK");
        bonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bonOKActionPerformed(evt);
            }
        });

        botDefault.setText("Default");
        botDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botDefaultActionPerformed(evt);
            }
        });

        botCancel.setText("Cancel");
        botCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botCancelActionPerformed(evt);
            }
        });

        buttonGroup1.add(radBtnHttp);
        radBtnHttp.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        radBtnHttp.setText("HTTP");

        buttonGroup1.add(radBtnFTP);
        radBtnFTP.setFont(new java.awt.Font("Microsoft Sans Serif", 0, 11)); // NOI18N
        radBtnFTP.setText("FTP");

        jLabel1.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 11)); // NOI18N
        jLabel1.setText("Upload Protocol ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblDevAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblUploadPath, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(bonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(botDefault, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(botCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(lblUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(lblPass, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                                            .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap())
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtuploadPath, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(radBtnHttp, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                        .addGap(48, 48, 48)
                                        .addComponent(radBtnFTP, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(txtIPAddress, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radBtnHttp, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(radBtnFTP, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblDevAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIPAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUploadPath, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtuploadPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblPass, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bonOK)
                    .addComponent(botDefault)
                    .addComponent(botCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
/*
    private void bonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bonOKActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bonOKActionPerformed

private void botDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botDefaultActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botDefaultActionPerformed

private void botCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botCancelActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botCancelActionPerformed
*/
     private void bonOKActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        txtIPAddress.setText(txtIPAddress.getText());
        txtPass.setText(txtPass.getText());
        txtuploadPath.setText(txtuploadPath.getText());
        txtUserName.setText(txtUserName.getText());
        username = txtUserName.getText();
        uploadAddress = txtIPAddress.getText();
        password = txtPass.getText();
        uploadPath = txtuploadPath.getText();
        if(radBtnHttp.isSelected() == true)
            MPFS2.uploadProtocol = MainMPFS.UPLOAD_PROTOCOL_OPTION.HTTP;
        else
            MPFS2.uploadProtocol = MainMPFS.UPLOAD_PROTOCOL_OPTION.FTP;
        setUploadSettingsPath();
        
        dispose();
    }

    private void botDefaultActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        txtIPAddress.setText("MCHPBOARD");
        txtPass.setText("microchip");
        txtuploadPath.setText("mpfsupload");
        txtUserName.setText("admin");
        username = "admin";
        uploadAddress = "MCHPBOARD";
        password = "microchip";
        uploadPath = "mpfsupload";
        radBtnHttp.setSelected(true);
        //MPFS2.outPutVersion = MainMPFS.MPFS_OUTPUT_VERSION.HTTP;
    }

    private void botCancelActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        txtIPAddress.setText(uploadAddress);
        txtPass.setText(password);
        txtuploadPath.setText(uploadPath);
        txtUserName.setText(username);
        if(MPFS2.uploadProtocol == MainMPFS.UPLOAD_PROTOCOL_OPTION.HTTP )
            radBtnHttp.setSelected(true);
        else
            radBtnFTP.setSelected(true);
        dispose();
    }
     public String getUploadPathStr()
    {
        str =  new String();
        str = MPFS2.GetProtocol();
        str = str+ "://"+ username + "@" + uploadAddress+ "/";
        str = str + "          ( ==> to modify ==> )";
        return str;
    }
    public void setUploadSettingsPath()
    {
       // str = new String();
        //str =  MPFS2.GetProtocol();
        MPFS2.txtUploadPath.setText(getUploadPathStr());
        //str = MPFS2.txtUploadPath.getText();
    }
    public String getUserName ()
    {
        return username;
    }
    public String getPasswordName ()
    {
        return password;
    }
    public String getIpAddress()
    {
        return uploadAddress;
    }
    public String getUploadPath()
    {
        return uploadPath;
    }
    public boolean getUploadProtocolradBut()
    {
        return radBtnHttp.isSelected();
    }
    public void setUploadProtocolRadBtn(int protocolSelection)
    {
        if(protocolSelection == 1)
        {
            radBtnHttp.setSelected(true);
            MPFS2.uploadProtocol = MainMPFS.UPLOAD_PROTOCOL_OPTION.HTTP;
        }
        else
        {
            radBtnFTP.setSelected(true);
            MPFS2.uploadProtocol = MainMPFS.UPLOAD_PROTOCOL_OPTION.FTP;
        }
    }

    public void setUploadAddress(String addr)
    {
        uploadAddress = addr;
        txtIPAddress.setText(addr);
    }

    public void setUploadPath(String addr)
    {
        uploadPath =  addr;
        txtuploadPath.setText(addr);
    }

    public void setUploadPass(String addr)
    {
        password  = addr;
        txtPass.setText(addr);
    }

    public void setUploadUserName(String addr)
    {
        username = addr;
        txtUserName.setText(addr);
    }

    void toolTipUploadSettings()
    {
        String infoString;
        /*infoString = "<html><body bgcolor=\"#FFFFCC\"><img src=\""+ getClass()
                     .getResource("/Resource/info.gif") + "\"> <b>";*/
        infoString = "<html><body bgcolor=\"#FFFFCC\"> <b>";

        infoString = infoString + "<headings> <font size=\"4\">"
                +" Upload Settings Help </font></headings></b><br>";
        txtIPAddress.setToolTipText(infoString +
                "Enter the network name by which the device <br>" +
                "can be accessed. NetBIOS hostnames DNS names,<br>" +
                " and IP addresses are all acceptable.<br>" +
                "the HTTP2 web server." + "</body></html>");

        txtuploadPath.setToolTipText(infoString +
                "Enter the upload path for MPFS2 images. <br>The default is" +
                " \"/mpfsuload\",<br>and can b" +
                "e changed in HTTP2.c." + "</body></html>");

        txtUserName.setToolTipText(infoString + "Enter the user name to access " +
                "the device. <br>The default is \"admin\"."+"</body></html>");

        txtPass.setToolTipText(infoString +"Enter the password to access " +
                "the device. <br>The default is \"microchip\"."
                +"</body></html>" );

        radBtnHttp.setToolTipText(infoString +
                "Build an MPFS2 image that is compatible <br>with " +
                "the HTTP2 web server.<br>STACK_USE_HTTP2_SERVER Should be enabled." + "</body></html>");

        radBtnFTP.setToolTipText(infoString +
                "Build a MPFS2 image that is compatible with<br>" +
                "the FTP server.STACK_USE_FTP_SERVER and <br>" +
                "HTTP_MPFS_UPLOAD_REQUIRES_AUTH should be enabled.<br>" +
                "TCP Socket TCP_PURPOSE_FTP_COMMAND and <br>" +
                "TCP_PURPOSE_FTP_DATA should be initialized." + "</body></html>");
    }


    private void UploadSettingKeyEventActionIntialization()
    {
        Action  ESCactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);
            dispose();
          }
        };
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true);
        JComponent comp = this.getRootPane();
        comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ESCAPE");
        ActionMap actionMap = comp.getActionMap();
        actionMap.put("ESCAPE", ESCactionListener);

        // OK button Action Solves MAC , Linux and Window enter key issue
        Action  OKactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            bonOKActionPerformed(actionEvent);
          }
        };
         // Cancel button Action Solves MAC , Linux and Window enter key issue
        Action  CancelactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            botCancelActionPerformed(actionEvent);
          }
        };
         // Default button Action Solves MAC , Linux and Window enter key issue
        Action  DefaultactionListener = new AbstractAction () {
          public void actionPerformed(ActionEvent actionEvent) {
            botDefaultActionPerformed(actionEvent);
            System.out.printf("i am here");
          }
        };
        KeyStroke enter_ok = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
        KeyStroke enter_default = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
        KeyStroke enter_cancel = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);

        bonOK.getInputMap(JButton.WHEN_FOCUSED).put(enter_ok, "OK");
        botDefault.getInputMap(JButton.WHEN_FOCUSED).put(enter_default, "deafult");
        botCancel.getInputMap(JButton.WHEN_FOCUSED).put(enter_cancel, "cancel");
        actionMap = bonOK.getActionMap();
        actionMap.put("OK", OKactionListener);
        bonOK.setActionMap(actionMap);

        actionMap = botDefault.getActionMap();
        actionMap.put("deafult", DefaultactionListener);
        botDefault.setActionMap(actionMap);

        actionMap = botCancel.getActionMap();
        actionMap.put("cancel", CancelactionListener);
        botCancel.setActionMap(actionMap);
    }

    /**
    * @param args the command line arguments
    */
    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                UploadSettings dialog = new UploadSettings(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bonOK;
    private javax.swing.JButton botCancel;
    private javax.swing.JButton botDefault;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblDevAddress;
    private javax.swing.JLabel lblPass;
    private javax.swing.JLabel lblUploadPath;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JRadioButton radBtnFTP;
    private javax.swing.JRadioButton radBtnHttp;
    private javax.swing.JTextField txtIPAddress;
    private javax.swing.JTextField txtPass;
    private javax.swing.JTextField txtUserName;
    private javax.swing.JTextField txtuploadPath;
    // End of variables declaration//GEN-END:variables

}
